using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.WebControls
{
    public class AcmsPagingControl : CompositeControl, IPostBackEventHandler, ICallbackEventHandler   
    {
        private bool useAjax;
        public bool UseAjax
        {
            get { return useAjax; }
            set { useAjax = value; }
        }
	
        private int currentPageIndex;
        public int CurrentPageIndex
        {
            get { return currentPageIndex; }
            set { currentPageIndex = value; }
        }

        private int totalRecords;
        public int TotalRecords
        {
            get { return totalRecords; }
            set { totalRecords = value; }
        }

        public int TotalPages
        {
            get
            {
                return (int)Math.Ceiling((decimal)TotalRecords / (decimal)PageSize);
            }
        }
        private int pageSize;
        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }

        private int blockSize;
        public int BlockSize
        {
            get { return blockSize; }
            set { blockSize = value; }
        }

        protected override object SaveViewState()
        {
            ViewState["CurrentPageIndex"] = CurrentPageIndex;
            ViewState["TotalRecords"] = TotalRecords;
            ViewState["PageSize"] = PageSize;
            ViewState["BlockSize"] = BlockSize;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            if (ViewState["CurrentPageIndex"] is int)
            {
                CurrentPageIndex = (int)ViewState["CurrentPageIndex"];
            }
            if (ViewState["TotalRecords"] is int)
            {
                TotalRecords = (int)ViewState["TotalRecords"];
            }
            if (ViewState["PageSize"] is int)
            {
                PageSize = (int)ViewState["PageSize"];
            }
            if (ViewState["BlockSize"] is int)
            {
                BlockSize = (int)ViewState["BlockSize"];
            }
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            int start = 0;
            int end = 0;
            
            if (BlockSize > 0 && currentPageIndex + 1 - BlockSize > 0)
            {
                start = BlockSize * (currentPageIndex / BlockSize);
            }
            if (BlockSize > 0 && TotalPages - start > BlockSize)
            {
                end = start + blockSize;
            }
            else
            {
                end = TotalPages;
            }

            while (BlockSize > 0 && TotalPages > BlockSize && end - start < BlockSize)
            {
                start--;
            }

            if (TotalPages > 1)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
                writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass);
                writer.RenderBeginTag(HtmlTextWriterTag.Div);

                if (CurrentPageIndex > 0)
                {
                    AddCommand("prev", "&laquo;", "Previous", CurrentPageIndex - 1, writer);
                }
                if (start > 0)
                {
                    int tmp = start - 1;
                    if (tmp < 0)
                    {
                        tmp = 0;
                    }
                    AddCommand("prevBlock", "...", string.Format("Previous {0} Pages", BlockSize), tmp, writer);
                }

                for (int i = start; i < end; i++)
                {
                    if (i == CurrentPageIndex)
                    {
                        writer.Write(string.Format("<b>{0}</b>", i + 1));
                    }
                    else
                    {
                        AddCommand(string.Concat("item", i), (i + 1).ToString(), string.Format("Page {0}", i + 1), i, writer);
                    }
                }

                if (end < TotalPages)
                {
                    int tmp = end;
                    if (tmp + BlockSize> totalRecords)
                    {
                        tmp = totalRecords - blockSize + 1;
                    }
                    AddCommand("nextBlock", "...", string.Format("Next {0} Pages", BlockSize), tmp, writer);
                }

                if (CurrentPageIndex < TotalPages - 1)
                {
                    AddCommand("next", "&raquo;", "Next", CurrentPageIndex + 1, writer);
                }

                writer.RenderEndTag();
            }
            else
            {
                return;
            }

        }

        private void AddCommand(string id, string text, string altText, int page, HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Id, string.Concat(this.ClientID, "_", id));
            if (UseAjax)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Href, string.Concat(this.Page.ClientScript.GetPostBackClientHyperlink(this, page.ToString())));
            }
            else
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Href, string.Concat("javascript:", this.Page.ClientScript.GetPostBackEventReference(this, page.ToString())));
            }
            writer.AddAttribute(HtmlTextWriterAttribute.Title, altText);
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write(text);
            writer.RenderEndTag();
        }


        #region IPostBackEventHandler Members

        public void RaisePostBackEvent(string eventArgument)
        {
            int pageNo = -1;
            try
            {
                pageNo = int.Parse(eventArgument);
            }
            catch
            {
            }
            if (pageNo >= 0)
            {
                RaiseBubbleEvent(this, new System.Web.UI.WebControls.CommandEventArgs("Pager", pageNo));
            }
        }

        #endregion

        #region ICallbackEventHandler Members

        public string GetCallbackResult()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void RaiseCallbackEvent(string eventArgument)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }
}
